﻿using AutoMapper;
using Infrastructure.Extend;
using Infrastructure.Web;
using MyData;
using MyData.ApiDto;
using MyData.Dto;
using MyData.Models;
using MyDomain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyApplication
{
    public class MobileApp
    {
        private IMobileRepository _service;
        public MobileApp(IMobileRepository Service)
        {
            _service = Service;
        }
        public PageDto<MobileDto> GetMobiles(QueryListModel query, string name)
        {
            var expression = ExtLinq.True<Mobile>();
            if (!string.IsNullOrEmpty(name))
            {
                expression = expression.And(t => t.Name.Contains(name));
            }
            var result = _service.IQueryable(expression).OrderByDescending(t => t.CreatedTime).Paging(query.Limit, query.Offset);
            return new PageDto<MobileDto>()
            {
                total = result.total,
                rows = Mapper.Map<List<Mobile>, IList<MobileDto>>(result.rows.ToList())
            };
        }
        public bool Save(Mobile mobile)
        {
            var entity = _service.FindEntity(mobile.Id);
            if (entity==null)
            {
                mobile.Id = Guid.NewGuid();
                return _service.Insert(mobile) > 0;
            }
            else
            {
                entity.Name = mobile.Name;
                entity.ImeiNum = mobile.ImeiNum;
                entity.TurnOver = mobile.TurnOver;
                
                entity.Ip = mobile.Ip;
                entity.Port = mobile.Port;
                entity.State = mobile.State;
                entity.LimitState = mobile.LimitState;
                entity.MonEnd = mobile.MonEnd;
                entity.MonStart = mobile.MonStart;
                entity.TueStart = mobile.TueStart;
                entity.TueEnd = mobile.TueEnd;
                entity.WedStart = mobile.WedStart;
                entity.WedEnd = mobile.WedEnd;
                entity.Similar = mobile.Similar;
                entity.DayStart = mobile.DayStart;
                entity.DayEnd = mobile.DayEnd;
                entity.NightSimilar = mobile.NightSimilar;
                entity.Live1 = mobile.Live1;
                entity.Live2 = mobile.Live2;
                entity.Duration = mobile.Duration;
                entity.DoorTime = mobile.DoorTime;
                entity.ThursStart = mobile.ThursStart;
                entity.ThursEnd = mobile.ThursEnd;
                entity.FriStart = mobile.FriStart;
                entity.FriEnd = mobile.FriEnd;
                entity.SatEnd = mobile.SatEnd;
                entity.SatStart = mobile.SatStart;
                entity.SunStart = mobile.SunStart;
                entity.SunEnd = mobile.SunEnd;
                entity.CameraIp = mobile.CameraIp;
                entity.CameraPort = mobile.CameraPort;
                entity.CameraName = mobile.CameraName;
                entity.CameraPass = mobile.CameraPass;
                return _service.Update(entity) > 0;
            }


        }
        public List<Mobile> GetMobiles()
        {
            return _service.IQueryable().ToList();
        }

        public MobileDto GetMobile(Guid Id)
        {
            var entity = _service.FindEntity(Id);
            return Mapper.Map<Mobile, MobileDto>(entity);
        }
        public bool MobileDelete(Guid Id)
        {
            var entity = _service.FindEntity(Id);
            return _service.Delete(entity) > 0;
        }
        public MobileLoginOput MobileLogin(MobileLoginInput input)
        {
            var entity = _service.FindEntity(c => c.ImeiNum == input.ImeiNum);
            if (entity == null)
            {
                entity = new Mobile();
                entity.Name = input.Ip;
                entity.Similar = 0.75f;
                entity.Duration = 5;
                entity.DoorTime = 5;
                entity.Live1 = 0.25f;
                entity.Live2 = 0.25f;
                entity.NightSimilar = 0.68f;
                entity.Id = Guid.NewGuid();
                entity.ImeiNum = input.ImeiNum;
                entity.Ip = input.Ip;
                entity.Port = input.Port;
                entity.LimitState = (int)EnumLimitType.关闭;
                entity.State = (int)EnumState.开启;
                _service.Insert(entity);
            }
            else
            {
                entity.Ip = input.Ip;
                entity.Port = input.Port;
                entity.CreatedTime = DateTime.Now;
                _service.Update(entity);
            }
            return Mapper.Map<Mobile, MobileLoginOput>(entity);

        }
        public Guid SaveMobile(Mobile mobile)
        {
            return _service.SaveMobile(mobile);
        }

    }
}
